package com.cisetech.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cisetech.mapper.GlobalMapper;
import com.cisetech.service.GlobalConfigService;
import com.cisetech.service.WeixinUtil;
import com.cisetech.vo.WeiXinConstant;
import com.cisetech.vo.globalconfig.GlobalConfigVo;


@Service
public class GlobalConfigServiceImpl implements GlobalConfigService{
    
    
    @Autowired
    GlobalMapper globalMapper;
    @Autowired
    WeixinUtil weixinUtil;
    @Override
    public GlobalConfigVo getGlobalConfig() {
    	GlobalConfigVo globalConfigVo=new GlobalConfigVo();
         // TODO Auto-generated method stub
    	globalConfigVo.setConfigKey("access_token");
         return globalMapper.get(globalConfigVo) ;
    }

    @Override
    public void updateGlobalConfig(GlobalConfigVo globalConfigVo) {
         // TODO Auto-generated method stub
    	globalMapper.update(globalConfigVo);
    }

    @Override
    public void insertGlobalConfig(GlobalConfigVo globalConfigVo) {
         // TODO Auto-generated method stub
    	globalMapper.insert(globalConfigVo);
    }

    @Override
    public void deleteGlobalConfig(GlobalConfigVo globalConfigVo) {
         // TODO Auto-generated method stub
    	globalMapper.delete(globalConfigVo);
    }

    @Override
    public List<GlobalConfigVo> getlistGlobalConfig() {
         // TODO Auto-generated method stub
         return null ;
    }

    @Override
    public String getAccessToken() {
         // TODO Auto-generated method stub
    	GlobalConfigVo globalConfigVo=new GlobalConfigVo();
    	 globalConfigVo.setConfigKey("access_token");
    	String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx6d0877900deea69d&secret=6f3e0ea55f66a0401d749d35d0f41607";
    	String str="";
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    	String date = sdf.format(new Date());
    	System.out.println("date================"+date);
    	GlobalConfigVo golbalConfig = globalMapper.get(globalConfigVo);
    	WeiXinConstant weixin=new WeiXinConstant();
    	if(golbalConfig==null||"".equals(golbalConfig)||"null".equals(golbalConfig)){
    		System.out.println(weixin.getAppID());
    		System.out.println(weixin.getAppSecret());
    		//url.replaceAll("APPID",weixin.getAppID()).replaceAll("APPSECRET", weixin.getAppSecret());
    		System.out.println(url);
    		JSONObject jsonObject = weixinUtil.httpRequest(url, "GET", "");
    		str=(String) jsonObject.get("access_token");
    		jsonObject.get("expires_in");
    		System.out.println("accessToken=================="+str);
    		GlobalConfigVo globalVo=new GlobalConfigVo();
    		globalVo.setConfigKey("access_token");
    		globalVo.setConfigValue(str);
    		globalVo.setCreateTime(date);
    		globalMapper.insert(globalVo);
    	}else {
    		url.replaceAll("APPID",weixin.getAppID()).replaceAll("APPSECRET", weixin.getAppSecret());
    		JSONObject jsonObject = weixinUtil.httpRequest(url, "GET", "");
    		str=(String) jsonObject.get("access_token");
    		jsonObject.get("expires_in");
    		GlobalConfigVo globalVo=new GlobalConfigVo();
    		globalVo.setUpdateTime(date);
    		globalVo.setConfigValue(str);
    		globalVo.setConfigKey("access_token");
    		globalMapper.update(globalVo);
		}
    	System.out.println(str);
		return str;
        
    }
}
